﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExampleProject.Models;

namespace ExampleProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
//            SqlSugarClientDB.DB.CodeFirst.InitTables(typeof(Nav));
            return View();
        }

        public ActionResult List()
        {
            var list = SqlSugarClientDB.DB.Queryable<Nav>().ToList();
            return View(list);
        }
        

        public ActionResult About()
        {
            ViewBag.Message = "itljfs@163.com";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Marage()
        {
            var list = SqlSugarClientDB.DB.Queryable<Nav>().ToList();

            return View(list);
        }
        [HttpGet]
        public ActionResult Edit(int? id,int fid=0)
        {
            if (id.HasValue&&id>0)
            {
                var model = SqlSugarClientDB.DB.Queryable<Nav>().First(m => m.Id == id);
                return View(model);
            }
            else
            {
                Nav model=new Nav(){FId = fid};
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult Edit(Nav input)
        {
            if (input.Id<1)
            {
                input.CTime=DateTime.Now;
                SqlSugarClientDB.DB.Insertable<Nav>(input).ExecuteCommand();
            }
            else
            {
                var model = SqlSugarClientDB.DB.Queryable<Nav>().First(m => m.Id == input.Id);
                model.Title = input.Title;
                model.Lev = input.Lev;
                model.FId = input.FId;
                model.Url = input.Url;
                model.Icon = input.Icon;
                SqlSugarClientDB.DB.Updateable<Nav>(model).ExecuteCommand();

            }
            return Json(true);
        }

        public ActionResult Del(int id)
        {
            SqlSugarClientDB.DB.Deleteable<Nav>(id).ExecuteCommand();
            return Json(true);
        }

    }
}